package com.htzos.base.parent.ctrl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.htzos.common.tool.ExtQueryBean;

public class BaseController {

	/**
	 * 成功
	 */
	public static final int SUCCESS=1;
	
	/**
	 * 失败
	 */
	public static final int FAIL=0;
	
	/**
	 * 输出JSON的相应 在json基础上追加 resultStatus
	 * 1表示成功  0，表示失败
	 * @author 徐德志
	 * @time 2016 2016年4月27日 上午10:39:20
	 * @parameter 
	 * @param response
	 * @param resultMsg
	 * @param result
	 */
	public void writeResultMessage(HttpServletResponse response,JSONObject resultMsg,Integer result){
		try {
			resultMsg.accumulate("resultStatus", result);
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.getWriter().print(resultMsg);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 回调普通消息在将普通消息存到json的message上 追加 resultStatus
	 * 1表示成功  0，表示失败
	 * @author xudz
	 * @time 2017 2017年1月16日 下午1:39:10
	 * @parameter 
	 * @param response
	 * @param resultMsg
	 * @param result
	 */
	public void writeResultMessage(HttpServletResponse response,String resultMsg,Integer result){
		try {
			JSONObject jsonObj=new JSONObject();
			jsonObj.accumulate("message", resultMsg);
			writeResultMessage(response, jsonObj, result);
		} catch (Exception e) {
			
		}
	}
	

	protected Map<String, Object> getSessionQueryString(HttpServletRequest request){
		Map<String, Object> map = null;
		HttpSession session = request.getSession(false);
		if(session != null) {
    		Object condition = session.getAttribute("conditionStr");
        	ExtQueryBean extQuery = (ExtQueryBean) session.getAttribute("extQueryStr");

        	if(condition != null || extQuery != null) {
	            map = new HashMap<String, Object>();
	        	if(condition != null){
					try {
						map.putAll(objectToMap(condition));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	        	}
	        	if(extQuery != null){
					try {
						map.putAll(objectToMap(extQuery));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	        	}
        	}
		}
		
		return map;
	}

	private static Map<String, Object> objectToMap(Object obj) throws Exception {    
        if(obj == null){    
            return null;    
        }   
   
        Map<String, Object> map = new HashMap<String, Object>();    
   
        Field[] declaredFields = obj.getClass().getDeclaredFields();    
        for (Field field : declaredFields) {    
            field.setAccessible(true);  
            Object value = field.get(obj);
            if(value != null && (value.getClass() != String.class || !value.equals("")))
            	map.put(field.getName(), field.get(obj));  
        }    
   
        return map;  
    }  
}
